package edu.jd.software.web.backend.controller;

import edu.jd.software.web.backend.common.api.CommonPage;
import edu.jd.software.web.backend.common.api.CommonResult;
import edu.jd.software.web.backend.domain.AdminRoleDto;
import edu.jd.software.web.backend.domain.RoleStatDto;
import edu.jd.software.web.backend.mbg.model.UmsAdmin;
import edu.jd.software.web.backend.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminService adminService;

    @PostMapping("/create")
    public CommonResult create(@RequestBody UmsAdmin entity) {
        adminService.create(entity);
        return CommonResult.success(null);
    }

    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsAdmin entity) {
        entity.setId(id);
        adminService.update(entity);
        return CommonResult.success(null);
    }

    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        adminService.delete(id);
        return CommonResult.success(null);
    }

    @GetMapping("/select/{id}")
    public CommonResult<UmsAdmin> select(@PathVariable Long id) {
        UmsAdmin entity = adminService.select(id);
        return CommonResult.success(entity);
    }

    @GetMapping("/listAll")
    public CommonResult<CommonPage<UmsAdmin>> listAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<UmsAdmin> list = adminService.listAll(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }



    @GetMapping("/list")
    public CommonResult<CommonPage<UmsAdmin>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(required = false) String username,
                                                   @RequestParam List<Integer> statusList) {
        List<UmsAdmin> list = adminService.list(pageNum, pageSize, username, statusList);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @GetMapping("/deleteByUsername")
    public CommonResult deleteByUsername(@RequestParam String username) {
        adminService.deleteByUsername(username);
        return CommonResult.success(null);
    }

    @GetMapping("/updateByIds")
    public CommonResult updateByIds(@RequestParam List<Long> ids,@RequestParam Integer status) {
        adminService.updateByIds(ids, status);
        return CommonResult.success(null);
    }


    @GetMapping("/subList")
    public CommonResult<List<UmsAdmin>> subList(@RequestParam Long roleId) {
        List<UmsAdmin> list = adminService.subList(roleId);
        return CommonResult.success(list);
    }

    @GetMapping("/groupList")
    public CommonResult<List<RoleStatDto>> groupList() {
        List<RoleStatDto> list = adminService.groupList();
        return CommonResult.success(list);
    }

    @GetMapping("/selectWithRoleList")
    public CommonResult<AdminRoleDto> selectWithRoleList(@RequestParam Long id) {
        AdminRoleDto adminRoleDto = adminService.selectWithRoleList(id);
        return CommonResult.success(adminRoleDto);
    }

}
